package wuppertal.bewegung;

import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WuppertalBewegungActivity extends Activity {
	WebView webview;
	
	private class WuppertalBewegungWebViewClient extends WebViewClient {
	    @Override
	    public boolean shouldOverrideUrlLoading(WebView view, String url) {
	        view.loadUrl(url);
	        return true;
	    }
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        webview = new WebView(this);
        webview.setWebViewClient(new WuppertalBewegungWebViewClient());
        setContentView(webview);
        
        if (isOnline())
        	webview.loadUrl("http://www.wuppertalbewegung-ev.de/mobile/");
        else
        {
        	String inet = getString(R.string.inet);
        	String summary = "<html><body style='text-align: center'><img src='data:image/png;base64,"+inet+"' /></body></html>";
        	webview.loadData(summary, "text/html", null);
        }
        webview.getSettings().setJavaScriptEnabled(true);
    }
    
    public boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }


	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
	    	webview.goBack();
	        return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}
}